package com.zbkj.crmeb.handler.orderconfirm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.utils.monitor.DingMsgEnum;
import com.utils.monitor.DingTalkMsgUtil;
import com.zbkj.crmeb.enums.OrderStatusEnum;
import com.zbkj.crmeb.plat.by.dto.ByCommonConstant;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.model.StoreOrderInfo;
import com.zbkj.crmeb.store.service.StoreOrderInfoService;
import com.zbkj.crmeb.store.service.StoreOrderService;
import com.zbkj.crmeb.task.plat.bieyang.dto.BieYangOrderConfirmDto;
import com.zbkj.crmeb.task.plat.poizon.PoizonApi;
import com.zbkj.crmeb.task.plat.poizon.dto.PoizonResponseBodyDto;
import com.zbkj.crmeb.task.plat.poizon.dto.ToizonOrderConfirmDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @author: wsl
 * @date: 2024/2/22 0022 22:28
 * @ClassName: PoizonOrderConfirmHandler
 */
@Component("poizonOrderConfirmHandler")
public class PoizonOrderConfirmHandler extends AbstractOrderConfirmHandler{
    @Autowired
    private StoreOrderService storeOrderService;
    @Autowired
    private StoreOrderInfoService storeOrderInfoService;
    @Autowired
    PoizonApi poizonApi;


    private static final Logger logger = LoggerFactory.getLogger(PoizonOrderConfirmHandler.class);

    @Override
    public boolean doPreCheck(OrderConfirmContext context) {
        boolean flag = super.doPreCheck(context);
        if (!flag){
            return false;
        }
        StoreOrderInfo storeOrderInfo = context.getStoreOrderInfo();
        return storeOrderInfo.getChildOrderNumber().startsWith("POIZON");
    }

    @Override
    public boolean doHandler(OrderConfirmContext context) {
        StoreOrderInfo storeOrderInfo = context.getStoreOrderInfo();
        Integer id = storeOrderInfo.getOrderId();
        StoreOrder storeOrder = storeOrderService.getOne(Wrappers.<StoreOrder>lambdaQuery().eq(StoreOrder::getId, id));
        String outOrderId = storeOrder.getOutOrderId();
        List<StoreOrderInfo> storeOrderInfos = storeOrderInfoService.list(Wrappers.<StoreOrderInfo>lambdaQuery().eq(StoreOrderInfo::getOrderId, id));
        // 校验子订单是否都是确认状态
        if (!validateChildrenOrderConfirm(storeOrderInfos)){
            return false;
        }
        // 向得物平台确认订单
        ToizonOrderConfirmDto dto = new ToizonOrderConfirmDto();
        dto.setChannelOrderNo(outOrderId);
        PoizonResponseBodyDto<PoizonResponseBodyDto.OrderConfirmDataDTO> responseBodyDto = poizonApi.orderConfirm(dto);
        logger.info("得物订单确认结果:{}",responseBodyDto);
        if (responseBodyDto.getCode() != 200){
            DingTalkMsgUtil.sendMsg(DingMsgEnum.PLAT,"得物订单确认失败:"+responseBodyDto.getMsg());
            return false;
        }
        DingTalkMsgUtil.sendMsg(DingMsgEnum.PLAT,"得物订单确认结果:"+responseBodyDto);
        return true;
    }

    private boolean validateChildrenOrderConfirm(List<StoreOrderInfo> storeOrderInfos) {
        if (CollectionUtils.isEmpty(storeOrderInfos)){
            return false;
        }
        for (StoreOrderInfo storeOrderInfo : storeOrderInfos) {
            if (Objects.equals(storeOrderInfo.getStatus(), OrderStatusEnum.CONFIRM.getCode())){
                return false;
            }
        }
        return true;
    }

}
